package com.message.tcp.ip.socket.builder;

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.packager.GenericPackager;

import com.message.tcp.ip.socket.util.MessageDefinition;
import com.message.tcp.ip.socket.util.UtilPackage;

public class EchoReqMessageBuilderImp implements ReqMessageBuilder {
	private static final String MTI = "0800";
	private GenericPackager packager = MessageDefinition.getGenericPackager();
	@Override
	public byte[] buildReqMessage() throws ISOException {
		return builEchoMessage();
	}
	public byte[] builEchoMessage() throws ISOException{
		return buildNetworkMessage(MessageDefinition.ECHO);
	}
	public byte[] buildSignOnMessage() throws ISOException {
		return buildNetworkMessage(MessageDefinition.SIGNON);
	}
	public byte[] buildSignOffMessage() throws ISOException{
		return buildNetworkMessage(MessageDefinition.SIGNOFF);
	}
	public byte[] buildKeyExchangeMessage() throws ISOException{
		return buildNetworkMessage(MessageDefinition.KEYEXC);
	}
	public byte[] buildStartAdviceMessage() throws ISOException{
		return buildNetworkMessage(MessageDefinition.STARTADV);
	}
	public byte[] buildStopAdviceMessage() throws ISOException{
		return buildNetworkMessage(MessageDefinition.STOPADV);
	}
	private byte[] buildNetworkMessage(String type) throws ISOException{
		String mti = MTI;
		String field07 = MessageDefinition.buildField07();
		String field11 = MessageDefinition.buildField11();
		String field37 = MessageDefinition.buildField37(field11);
		String field70 = type;
		ISOMsg isoMsg = new ISOMsg();
		isoMsg.setPackager(packager);
		isoMsg.setMTI(mti);
		isoMsg.set( 7, field07);
		isoMsg.set(11, field11);
		isoMsg.set(37, field37);
		isoMsg.set(70, field70);
		byte[] pack=isoMsg.pack();
		System.out.println(new String(pack));
		return UtilPackage.addHeader(pack);
	}
}
